﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;
using CustomSecurity;
namespace Lists
{
    public class FunctionForEdit : DoiTuong
    {
        #region ParentFunctionID property
        public Guid ParentFunctionID
        {
            get
            {
                return (Guid)_DataRow["ParentFunctionID"];
            }
            set
            {
                _DataRow["ParentFunctionID"] = value;
            }
        }
        #endregion
       
        #region Name property
        public string Name
        {
            get
            {
                return (string)_DataRow["Name"];
            }
            set
            {
                _DataRow["Name"] = value;
            }
        }
        #endregion

        #region Description property
        public string Description
        {
            get
            {
                return (string)_DataRow["Description"];
            }
            set
            {
                _DataRow["Description"] = value;
            }
        }
        #endregion

        #region PosibleRight property
        public int PosibleRight
        {
            get
            {
                return (int)_DataRow["PosibleRight"];
            }
            set
            {
                _DataRow["PosibleRight"] = value;
            }
        }
        #endregion

        public FunctionForEdit(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucFunctionForEdit : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucFunctionForEdit _Instance;
        public static DanhMucFunctionForEdit Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucFunctionForEdit();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Function";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new FunctionForEdit((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public FunctionForEdit this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new FunctionForEdit(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public FunctionForEdit NewDoiTuong()
        {
            return new FunctionForEdit(DataTable.NewRow());
        }

        public FunctionForEdit GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new FunctionForEdit(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public FunctionForEdit GetByName(string name)
        {
            return GetByProperty("Name", name);
        }

        public FunctionForEdit GetByFunctionForEditID(Guid FunctionID)
        {
            return GetByProperty("ID", FunctionID);
        }

        public static DanhMucFunctionForEdit GetAll()
        {
            return Instance;
        }

        public static DanhMucFunctionForEdit Filter(string filterExpression)
        {
            try
            {
                DanhMucFunctionForEdit obj = new DanhMucFunctionForEdit();
                obj.DataView = new DataView(DanhMucFunctionForEdit.Instance.DataTable, filterExpression, "ID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

    }
}